import 'dart:async';

import 'package:chathouse/pages/expert/buyWallet/recharge_bank_card.dart';
import 'package:chathouse/pages/login/select_role_page.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/log.dart';
import 'package:chathouse/widgets/base_appbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';

import '../../../model/response_bean.dart';
import '../../../push/chat_house_push.dart';
import '../../../util/base_utils.dart';
import '../../../widgets/form_widgets/form_widgets.dart';
import '../../../widgets/my_input_widget.dart';

/// @class : motify_pwd
/// @date : 2023-03-02 20:37
/// @name : LFC
/// @description : 重设密碼
class UpdatePwdPage extends StatefulWidget {
  int type = 1; //1 用戶 2專家
  UpdatePwdPage(this.type, {Key? key}) : super(key: key);

  @override
  State<UpdatePwdPage> createState() => _UpdatePwdPageState();
}

class _UpdatePwdPageState extends State<UpdatePwdPage> {
  TextEditingController oldPwdController = TextEditingController();
  TextEditingController newPwdController = TextEditingController();
  TextEditingController confirmPwdController = TextEditingController();
  late StreamSubscription<bool> keyboardSubscription;
  bool keyboardVisible = false;

  @override
  void initState() {
    super.initState();
    var keyboardVisibilityController = KeyboardVisibilityController();
    // Subscribe
    keyboardSubscription =
        keyboardVisibilityController.onChange.listen((bool visible) {
      keyboardVisible = visible;
    });
  }

  @override
  void dispose() {
    keyboardSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var keyboardSize = MediaQuery.of(context).viewInsets.bottom;
    return Scaffold(
      // resizeToAvoidBottomInset: false,
      backgroundColor: Colors.white,
      appBar: getAppBar(
          title: "重設密碼",
          titleTextStyle:
              getTextStyle(34.sp, color: Colors.black, fontType: FontType.MID)),
      body: ListView(
        children: [
      MyInputWidget(
        obscureText: true,
        textStyle: getTextStyle(18.sp, fontType: FontType.MID),
        title: "現有密碼",
        controller: oldPwdController,
      ).setContainer(margin: EdgeInsets.only(top: 26.w)),
      MyInputWidget(
        obscureText: true,
        title: "新密碼",
        textStyle: getTextStyle(18.sp, fontType: FontType.MID),
        controller: newPwdController,
      ).setContainer(margin: EdgeInsets.only(top: 26.w)),
      MyInputWidget(
        obscureText: true,
        title: "確認密碼",
        textStyle: getTextStyle(18.sp, fontType: FontType.MID),
        controller: confirmPwdController,
      ).setContainer(margin: EdgeInsets.only(top: 26.w)),
      FormWidgets.getTextButton(
              "確認",
              widget.type == 1
                  ? MyColors.green_69157608
                  : MyColors.green_69157154)
          .setContainer(
              margin: EdgeInsets.only(
                  bottom: 60.w, left: 60.w, right: 60.w, top: 20.w))
          .setOnClickListener(() {
        checkInput();
      }, isInkWell: true),
      SizedBox(
        height: keyboardVisible ? keyboardSize : 0,
      ),
        ],
      ).setContainer(padding: EdgeInsets.symmetric(horizontal: 34.w)),
    );
  }

  void checkInput() {
    String oldPwd = oldPwdController.text.toString();
    String newPwd = newPwdController.text.toString();
    String confirmPwd = confirmPwdController.text.toString();

    if (oldPwd.isNullOrEmpty ||
        newPwd.isNullOrEmpty ||
        confirmPwd.isNullOrEmpty) {
      ToastUtil.showToast("請輸入完整密碼");
      return;
    }

    Obj.showLoading();
    final params = {
      "MembersPassword": oldPwd,
      "newPassword": newPwd,
      "rawPassword": confirmPwd,
    };
    Apis.resetPassword(params).then((value) {
      Obj.dismissLoading();
      var result = ResponseBean.fromJson(value?.data ?? {});
      LogUtil.d("resetPassword success $value");
      if (result.isRequestSuccess) {
        FocusManager.instance.primaryFocus?.unfocus();
        SpUtil.clear();
        Future.delayed(const Duration(milliseconds: 500), () {
          SpUtil.clear();
          //初始化極光
          ChatHousePush.instance.initPlatformState();
          Get.offAll(const SelectRolePage());
        });
        ToastUtil.showToast("修改密碼成功");
      }
    }).catchError((onError) {
      Obj.dismissLoading();
      LogUtil.d("resetPassword error $onError");
    });
  }
}
